<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;
use Exception;

class OperationRecord extends Model
{
    public $timestamps = false;
    protected $table = 'operation_record';

    //类型
    const TYPE_DAT_TASKS = 1;
    const TYPE_SIGN = 2;
    const TYPE_OPEN_TREASURE = 3;
    const TYPE_WITHDRAW = 4;
    const TYPE_GAME = 5;

    static $TypyDisplay = [
        self::TYPE_DAT_TASKS => '日常任务',
        self::TYPE_SIGN => '签到',
        self::TYPE_OPEN_TREASURE => '开宝箱',
        self::TYPE_WITHDRAW => '提现',
        self::TYPE_GAME => '游戏',
    ];

    //状态
    const STATUS_WAITING = 1;
    const STATUS_YES = 2;
    const STATUS_NO = 3;

    static $StatusDisplay = [
        self::STATUS_WAITING => '待审核',
        self::STATUS_YES => '审核通过',
        self::STATUS_NO => '审核不通过',
    ];

    //提现类型
    const REFLECT_TYPE_ALIPAY = 1;
    const REFLECT_TYPE_WECHAT = 2;
    const REFLECT_TYPE_RECHARGE = 3;
    const REFLECT_TYPE_EXCHANGE = 4;
    const REFLECT_TYPE_REAL = 6;

    static $ReflectTypeDisplay = [
        self::REFLECT_TYPE_ALIPAY => '红包提现到支付宝',
        self::REFLECT_TYPE_WECHAT => '红包提现到微信',
        self::REFLECT_TYPE_RECHARGE => '话费充值',
        self::REFLECT_TYPE_EXCHANGE => '红包兑换',
        self::REFLECT_TYPE_REAL => '兑换实物',
    ];

    //

    /**
     * 提现到支付宝
     * @param $operationRecord
     * @return array
     */
    static function WithdrawAlipay($operationRecord)
    {
        DB::beginTransaction();

        $operationRecord->status = OperationRecord::STATUS_YES;
		$operationRecord->tixian_time = date("Y-m-d H:i:s",time());
        if (!$operationRecord->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '提交失败'];
        }

        $noviceTask = NoviceTaskRecord::RecordPublish($operationRecord->user_id, NoviceTaskRecord::TYPE_WITHDRAW);
        if ($noviceTask['status'] !== 1) {
            DB::rollBack();

            return ['status' => 2, 'message' => '提交失败'];
        }
        DB::commit();

        return ['status' => 1, 'message' => '提交成功'];
    }

//

    /**
     * 提现到微信
     * @param $operationRecord
     * @return array
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Exception
     */
    static function WithdrawWechat($operationRecord)
    {
        DB::beginTransaction();

        $operationRecord->status = OperationRecord::STATUS_YES;
		$operationRecord->tixian_time = date("Y-m-d H:i:s",time()); 
        if (!$operationRecord->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '提交失败'];
        }
        $pay = Wechat::ToBalance($operationRecord->order_number, bcmul($operationRecord->cash, 100),$operationRecord->user_id);
        if ($pay['status'] !== 1) {
            DB::rollBack();

            return $pay;
        }

        $noviceTask = NoviceTaskRecord::RecordPublish($operationRecord->user_id, NoviceTaskRecord::TYPE_WITHDRAW);
        if ($noviceTask['status'] !== 1) {
            DB::rollBack();

            return ['status' => 2, 'message' => '提交失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '提交成功'];
    }

    /**
     * 微信批量提现
     * @param $ids
     * @return array
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \Exception
     */
    static function WechatAll($ids)
    {
        $return['failed'] = [];
        $return['success'] = [];
        foreach ($ids as $id) {
            $operation = OperationRecord::find($id);
            if (!$operation) {
                dd($id);
            }
            $re = self::WithdrawWechat($operation);
            if ($re['status'] != 1) {
                $return['failed'][] = $id;
            } else {
                $return['success'][] = $id;
            }
        }
        if ($n = count($return['failed'])) {
            return ['status' => 2, 'message' => $n . '个失败了', 'ids' => $return['success']];
        }
        return ['status' => 1, 'message' =>  '成功', 'ids' => $return['success']];
    }

//

    /**
     * 话费充值
     * @param $operationRecord
     * @return array
     */
    static function PhoneRecharge($operationRecord)
    {
        DB::beginTransaction();

        $operationRecord->status = OperationRecord::STATUS_YES;
        if (!$operationRecord->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '提交失败'];
        }

        $noviceTask = NoviceTaskRecord::RecordPublish($operationRecord->user_id, NoviceTaskRecord::TYPE_WITHDRAW);
        if ($noviceTask['status'] !== 1) {
            DB::rollBack();

            return ['status' => 2, 'message' => '提交失败'];
        }
        DB::commit();

        return ['status' => 1, 'message' => '提交成功'];
    }


    /**
     * 提现不通过
     * @param $operationRecord_id
     * @param $check_content
     * @return array
     * @throws \Exception
     */
    static function WithdrawOff($operationRecord_id, $check_content)
    {
        DB::beginTransaction();

        $operationRecord = OperationRecord::findOrFail($operationRecord_id);
        //修改状态
        $operationRecord->status = OperationRecord::STATUS_NO;
        $operationRecord->check_content = $check_content;
        if (!$operationRecord->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '提交失败'];
        }

        //返回金额
        if ($operationRecord->reflect_type === self::REFLECT_TYPE_RECHARGE || $operationRecord->reflect_type === self::REFLECT_TYPE_REAL) {
            //如果是话费充值返回金币
            $coin_number = DB::table('t_member')->where('c_id', $operationRecord->user_id)->value('coin_number');
            $num_all = bcadd($operationRecord->number,$coin_number);
            $update = DB::table('t_member')->where('c_id', $operationRecord->user_id)->update([
                'coin_number' => $num_all
            ]);
//        } else if () {
//            //实物
//            $coin_number = DB::table('t_member')->where('c_id', $operationRecord->user_id)->value('coin_number');
//            $num_all = bcadd($operationRecord->cash,$coin_number);
//            $update = DB::table('t_member')->where('c_id', $operationRecord->user_id)->update([
//                'coin_number' => $num_all
//            ]);
        } else {
            //提现则将人民币返回
            $cash_number = DB::table('t_member')->where('c_id', $operationRecord->user_id)->value('cash_number');
            $num_all = bcadd($operationRecord->number,$cash_number,2);
            $update = DB::table('t_member')->where('c_id', $operationRecord->user_id)->update([
                'cash_number' => $num_all
            ]);
        }
        if (!$update) {
            DB::rollBack();

            return ['status' => 2,'message' => '提交失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '成功'];

    }


    /**
     * 操作积分管理修改
     * @param $id
     * @param $request
     * @return array
     */
    static function TasksEdit($id, $request)
    {
        try {

            if (!DB::table('day_tasks')->where('id', $id)->update([
                'name' => $request->name,
                'number' => $request->number,
                'frequency' => $request->frequency,
                'content' => $request->content,
                'add_number' => $request->add_number
            ])) {
                return ['status' => 2, 'message' => '没有修改'];
            }

            return ['status' => 1, 'message' => '成功'];
        } catch (Exception $exception) {
            return ['status' => 1, 'message' => '成功'];
        }
    }
}
